import axios from 'axios'
import store from '@/store'
import { getTokenFUNC } from '@/utils/auth'

const BASEURL = process.env.VUE_APP_API_ROOT
// === 'production' ? 'https://huadragonfly.booxf.site/hua/v1' : 'http://127.0.0.1:8000/hua/v1'
// const BASEURL = 'https://huadragonfly.booxf.site/hua/v1'
// create an axios instance
const service = axios.create({
    baseURL: BASEURL, // url = base url + request url
    // withCredentials: true, // send cookies when cross-domain requests
    timeout: 5000 // request timeout
})

// request interceptor
service.interceptors.request.use(
    config => {
        // do something before request is sent

        if (store.getters.token) {
            // let each request carry token
            // ['X-Token'] is a custom headers key
            // please modify it according to the actual situation
            config.headers['Authorization'] = getTokenFUNC()
        }
        return config
    },
    // error => {
    //     // do something with request error
    // }
)

// / 响应拦截器
service.interceptors.response.use(
    /**
     * If you want to get http information such as headers or status
     * Please return  response => response
     */

    /**
     * Determine the request status by custom code
     * Here is just an example
     * You can also judge the status by HTTP Status Code
     */
    response => {
        return response
        // let repData = decryptDes(response.data)
        // // repData = reqData.replace(/'/g,"\'")
        // repData = JSON.parse(repData)
        // const message = repData.errMsg
        // const code = repData.code
        // if (code !== 200) { // 改成后端响应成功的状态码
        //     switch (code) { // 响应错误判断
        //         case 401:
        //             if (router.currentRoute.name === 'login') {
        //                 return Promise.reject(new Error(message))
        //             } else {
        //                 Message({
        //                     message: '您没有权限访问该资源',
        //                     type: 'error'
        //                 })
        //             }
        //             break
        //         case 304:
        //             Message({
        //                 message: '密令过期了，请重新登录！',
        //                 type: 'error'
        //             })
        //             break
        //         case 500:
        //             Message({
        //                 message: '服务异常，请联系管理员',
        //                 type: 'error'
        //             })
        //             break
        //         default:
        //             Message({
        //                 message: message,
        //                 type: 'error'
        //             })
        //     }
        //     // store.dispatch('user/resetToken').then(() => {
        //     //   location.reload()  // 重复登录的情况
        //     // })
        //     return Promise.reject(new Error(message || 'Error'))
        // } else {
        //     return repData
        // }
    },
    error => {
        this.$notify({
            title: error.message,
            dangerouslyUseHTMLString: true
        })
        return Promise.reject(error)
    }
)

export default service
